using UnityEngine;
using System.Collections;

public class Lixo : MonoBehaviour 
{
	private int id = 1;
	private Vector2 screen;

    void Start()
    {
		this.gameObject.AddComponent("MeshRenderer");
        this.gameObject.AddComponent("MeshFilter");
		this.gameObject.AddComponent("BoxCollider");
		this.gameObject.AddComponent("Rigidbody");

		this.screen.y = GameObject.Find("Main Camera").GetComponent<Camera>().orthographicSize;
		this.screen.x = this.screen.y * GameObject.Find("Main Camera").GetComponent<Camera>().aspect;

        Mesh mesh = Quad.Create(this.screen.y * 0.1f, this.screen.y * 0.1f);

        this.gameObject.GetComponent<MeshFilter>().mesh = mesh;
		this.gameObject.GetComponent<BoxCollider>().size = new Vector3(this.screen.y * 0.2f, this.screen.y * 0.2f , 0.4f);
        this.gameObject.GetComponent<MeshRenderer>().material = AtlasJogo.globalMaterial;
		this.gameObject.GetComponent<Rigidbody>().useGravity  = false;
		
		this.transform.position = new Vector3(this.screen.x + (this.screen.x * 0.2f), -1.5f, 0);
		this.tag = "Obstaculo";
    }

    public void Update()
    {
        this.gameObject.GetComponent<MeshFilter>().mesh.uv = new Vector2[]
		{
			new Vector2(AtlasJogo.rects[id].xMin, AtlasJogo.rects[id].yMax),
			new Vector2(AtlasJogo.rects[id].xMax, AtlasJogo.rects[id].yMax),
			new Vector2(AtlasJogo.rects[id].xMax, AtlasJogo.rects[id].yMin),
			new Vector2(AtlasJogo.rects[id].xMin, AtlasJogo.rects[id].yMin)
		};
		
		if(this.transform.position.x < -this.screen.x - (this.screen.x * 0.2f))
		{
			Destroy(this.gameObject);	
			GameObject.Find("Cena").GetComponent<Jogo>().pulados++;
		}
    }
	
	void FixedUpdate()
	{
		this.transform.Translate(-0.2f, 0, 0);			
	}
}
